package com.lingjtx.auth.handle;

import com.lingjtx.auth.service.UserHandleService;
import com.lingjtx.common.core.constant.Const;
import com.lingjtx.common.core.exception.ResponseErrorException;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserServiceContext {

    private final List<UserHandleService> contexts = new ArrayList<>();

    public void setHandlers(UserHandleService handle) {
        contexts.add(handle);
    }

    public UserHandleService getHandle(String type) {
        for (UserHandleService context : contexts) {
            if (context.getUserType().equals(type)) {
                return context;
            }
        }
        throw new ResponseErrorException(String.format(" %s 为空，请指定用户类型！", Const.AuthTokenMap.USER_TYPE));
    }
}
